/*
 * Copyright © Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ygxsk.carhome.permission;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;
import com.yanzhenjie.permission.runtime.Permission;
import com.ygxsk.carhome.R;

import java.util.List;

/**
 * Created by YanZhenjie on 2018/1/1.
 */
public final class RuntimeRationale implements Rationale<List<String>> {

    /**
     * 保证弹窗唯一
     */
    MaterialDialog materialDialog;

    /**
     * 这个是保证仅仅只有一个弹窗的用户体验
     */
    /*public void closeDialog(){
        //retain
    }*/
    @Override
    public void showRationale(Context context, List<String> permissions, final RequestExecutor executor) {

        List<String> permissionNames = Permission.transformText(context, permissions);
        String message = context.getString(R.string.message_permission_rationale, TextUtils.join("\n", permissionNames));

        //LogUtils.d("itchen----RuntimeRationale-->"+message);

        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title("使用提示：")
                .content(message)
                .positiveText("去允许")
                .positiveColor(ContextCompat.getColor(context, R.color.couponpayment))
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        executor.execute();
                        //LogUtils.d("itchen----RuntimeRationale-->执行允许权限弹窗");
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        executor.cancel();
                    }
                }).cancelable(false);

        if (materialDialog != null) {
            if (materialDialog.isShowing()) {
                materialDialog.dismiss();
                materialDialog = null;
            }
        }

        if (ActivityUtils.isActivityAlive(context)) {
            materialDialog = builder.show();
        }

        //2018年06月05日发现该处代码有取消，反复取消直接导致权限失败内容
        /*new AlertDialog.Builder(context)
                .setCancelable(false)
                .setTitle(R.string.title_dialog)
                .setMessage(message)
                .setPositiveButton(R.string.resume, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        executor.execute();//反复提示执行
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        executor.cancel();//取消掉
                    }
                })
                .show();*/
    }
}